package com.haipumi.servlets.backadmin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.haipumi.beans.DataGrid;
import com.haipumi.beans.JsonModel;
import com.haipumi.beans.haipumi_task;
import com.haipumi.biz.IBackTaskBiz;
import com.haipumi.biz.impl.BackTaskBizImpl;
import com.haipumi.servlets.CommonServlet;
import com.haipumi.utils.CommonData;

public class BackTaskServlet extends CommonServlet {

	IBackTaskBiz ibtb=new BackTaskBizImpl();
	/**
	 * 
	 */
	private static final long serialVersionUID = 3498134975698225891L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String op=null;
		op=request.getParameter("op");
		if(!"".equals(op)&&op!=null){
			if("findTask".equals(op)){
				findTask(request,response);
			}else if("addTask".equals(op)){
				addTask(request,response);
			}else if("updateTask".equals(op)){
				updateTask(request,response);
			}else if("checkTask".equals(op)){
				checkTask(request,response);
			}else if("caozuotask".equals(op)){
				caozuotask(request,response);
			}else if("finishtask".equals(op)){
				finishtask(request,response);
			}
		}
	}

	private void finishtask(HttpServletRequest request,
			HttpServletResponse response) {
		String tid=request.getParameter("tid");
		boolean flag=false;
		flag=ibtb.finishTask(tid);
		JsonModel jm=new JsonModel();
		if(flag){
			jm.setCode(1);
			jm.setMsg("修改成功，任务完成。。。");
		}
	}

	private void caozuotask(HttpServletRequest request,
			HttpServletResponse response) {
		String tid=request.getParameter("tid");
		String type=request.getParameter("type");
		boolean flag=false;
		flag=ibtb.caozuo(tid, type);
		JsonModel jm=new JsonModel();
		if(flag){
			jm.setCode(1);
			jm.setMsg("成功执行...");
		}else {
			jm.setCode(0);
			jm.setMsg("操作失败，请联系管理员...");
		}
		super.outData(response, jm);
	}

	private void checkTask(HttpServletRequest request,
			HttpServletResponse response) {
		String uid=request.getParameter("uid");
		haipumi_task task=null;
		task=ibtb.checkTask(uid);
		//TODO 任务数加1, 要放在审批处理后
		int taskcount=(Integer) request.getSession().getServletContext().getAttribute(CommonData.APPLICATIONTASKCOUNT);
		request.getSession().getServletContext().setAttribute(CommonData.APPLICATIONTASKCOUNT, (taskcount+1) );
		super.outData(response, task);
		
	}

	private void updateTask(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO 更新任务，暂时不
		
	}

	private void addTask(HttpServletRequest request,
			HttpServletResponse response) {
		// TODO  后台添加官方发布
		
	}

	private void findTask(HttpServletRequest request,
			HttpServletResponse response) {
		List<haipumi_task>tasklist=ibtb.findAllTask();
		int r=0;
		r=ibtb.countAllTask();
		DataGrid dg=new DataGrid();
		dg.setRows(tasklist);
		dg.setTotal(r);
		super.outData(response, dg);
	}

}
